package sdkshell.kuaiyan.com.kuaiyansdk.manager;

import android.content.Context;

import com.wmcsk.dl.dl.internal.DLPluginManager;
import com.wmcsk.dl.dl.internal.DLPluginPackage;
import com.wmcsk.dl.listener.AppDataStatisticsPluginInitble;
import com.wmcsk.dl.listener.DataStatisticsPluginInitable;
import com.wmcsk.dl.listener.Initable;
import com.wmcsk.dl.listener.MenuPluginInitable;
import com.wmcsk.dl.listener.PluginAppDataStatisticsInitOkListener;
import com.wmcsk.dl.listener.PluginDataStatisticsInitOkListener;
import com.wmcsk.dl.listener.PluginInitOkListener;
import com.wmcsk.dl.listener.PluginMenuInitOkListener;
import com.wmcsk.dl.listener.PluginSelectShareInitOkListener;
import com.wmcsk.dl.listener.PluginSignInitOkListener;
import com.wmcsk.dl.listener.PluginVideoShadowInitOkListener;
import com.wmcsk.dl.listener.SelectSharePluginInitable;
import com.wmcsk.dl.listener.SignPluginInitable;
import com.wmcsk.dl.listener.VideoShadowPluginInitable;
import com.wmcsk.dl.util.LogUtils;

import java.io.File;

import dalvik.system.DexClassLoader;
import sdkshell.kuaiyan.com.kuaiyansdk.BuildConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.SdkInit;
import sdkshell.kuaiyan.com.kuaiyansdk.bean.ApkNames;
/**
 * Created by wm on 2017/7/11.
 */
public class KyInterfacePull {
    private static KyInterfacePull kyInterfacePull;
    private KyInterfacePull() {

    }
    public static KyInterfacePull getInstance(){
        if(kyInterfacePull == null){
            synchronized (KyInterfacePull.class){
                if(kyInterfacePull == null){
                    kyInterfacePull = new KyInterfacePull();
                }
            }
        }
        return kyInterfacePull;
    }

    protected void createKySdkInterfaceFromPlugin(String name, PluginInitOkListener pluginInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof Initable) {
                    Initable initable = (Initable) object;
                    initable.init(SdkInit.getContext(),pluginInitOkListener);
                } else {
                    if(pluginInitOkListener!=null){
                        pluginInitOkListener.initError(null,"sdk inner error ! object not initable");
                    }
                    LogUtils.getNewInstance("KyInterfacePull").eob("sdk inner error ! object not initable");
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginInitOkListener!=null){
                    pluginInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("KyInterfacePull").eob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginInitOkListener!=null){
                pluginInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("KyInterfacePull").eob("sdk inner error ! plugin not loaded");
        }
    }
    public void init(String name, PluginVideoShadowInitOkListener pluginVideoShadowInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginVideoShadowInitOkListener);
    }
    public void init(String name, PluginInitOkListener pluginInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginInitOkListener);
    }
    public void init(String name, PluginSignInitOkListener pluginInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginInitOkListener);
    }
    public void init(String name, PluginMenuInitOkListener pluginMenuInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginMenuInitOkListener);
    }
    public void init(String name, PluginSelectShareInitOkListener pluginSelectShareInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginSelectShareInitOkListener);
    }
    public void init(String name, PluginDataStatisticsInitOkListener pluginSelectShareInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginSelectShareInitOkListener);
    }
    public void init(String name, PluginAppDataStatisticsInitOkListener pluginSelectShareInitOkListener){
        createKySdkInterfaceFromPlugin(name,pluginSelectShareInitOkListener);
    }

    protected void createKySdkInterfaceFromPlugin(String name, PluginAppDataStatisticsInitOkListener pluginAppDataStatisticsInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof AppDataStatisticsPluginInitble) {
                    AppDataStatisticsPluginInitble initable = (AppDataStatisticsPluginInitble) object;
                    initable.init(SdkInit.getContext(),pluginAppDataStatisticsInitOkListener);
                } else {
                    if(pluginAppDataStatisticsInitOkListener!=null){
                        pluginAppDataStatisticsInitOkListener.initError(null,"sdk inner error ! object not initable");
                    }
                    LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! object not initable");
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginAppDataStatisticsInitOkListener!=null){
                    pluginAppDataStatisticsInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginAppDataStatisticsInitOkListener!=null){
                pluginAppDataStatisticsInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! plugin not loaded");
        }
    }


    protected void createKySdkInterfaceFromPlugin(String name, PluginDataStatisticsInitOkListener pluginDataStatisticsInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof DataStatisticsPluginInitable) {
                    DataStatisticsPluginInitable initable = (DataStatisticsPluginInitable) object;
                    initable.init(SdkInit.getContext(),pluginDataStatisticsInitOkListener);
                } else {
                    if(pluginDataStatisticsInitOkListener!=null){
                        pluginDataStatisticsInitOkListener.initError(null,"sdk inner error ! object not initable");
                    }
                    LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! object not initable");
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginDataStatisticsInitOkListener!=null){
                    pluginDataStatisticsInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginDataStatisticsInitOkListener!=null){
                pluginDataStatisticsInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! plugin not loaded");
        }
    }

    protected void createKySdkInterfaceFromPlugin(String name, PluginVideoShadowInitOkListener pluginVideoShadowInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof VideoShadowPluginInitable) {
                    VideoShadowPluginInitable initable = (VideoShadowPluginInitable) object;
                    initable.init(SdkInit.getContext(),pluginVideoShadowInitOkListener);
                } else {
                    if(pluginVideoShadowInitOkListener!=null){
                        pluginVideoShadowInitOkListener.initError(null,"sdk inner error ! object not initable");
                    }
                    LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! object not initable");
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginVideoShadowInitOkListener!=null){
                    pluginVideoShadowInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginVideoShadowInitOkListener!=null){
                pluginVideoShadowInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! plugin not loaded");
        }
    }
    protected void createKySdkInterfaceFromPlugin(String name,PluginMenuInitOkListener pluginMenuInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof MenuPluginInitable) {
                    MenuPluginInitable initable = (MenuPluginInitable) object;
                    initable.init(SdkInit.getContext(),pluginMenuInitOkListener);
                } else {
                    if(pluginMenuInitOkListener!=null){
                        pluginMenuInitOkListener.initError(null,"sdk inner error ! object not initable");
                    }
                    LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! object not initable");
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginMenuInitOkListener!=null){
                    pluginMenuInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginMenuInitOkListener!=null){
                pluginMenuInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! plugin not loaded");
        }
    }
    protected void createKySdkInterfaceFromPlugin(String name, PluginSelectShareInitOkListener pluginSelectShareInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof SelectSharePluginInitable) {
                    SelectSharePluginInitable initable = (SelectSharePluginInitable) object;
                    initable.init(SdkInit.getContext(),pluginSelectShareInitOkListener);
                } else {
                    if(pluginSelectShareInitOkListener!=null){
                        pluginSelectShareInitOkListener.initError(null,"sdk inner error ! object not initable"+object.getClass());
                    }
                    LogUtils.getNewInstance("PluginSelectShareInitOkListener").dob("sdk inner error ! object not initable"+object.getClass());
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginSelectShareInitOkListener!=null){
                    pluginSelectShareInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("PluginSelectShareInitOkListener").dob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginSelectShareInitOkListener!=null){
                pluginSelectShareInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("PluginSelectShareInitOkListener").dob("sdk inner error ! plugin not loaded");
        }
    }
    protected void createKySdkInterfaceFromPlugin(String name, PluginSignInitOkListener pluginInitOkListener){
        String initClazz = ApkNames.getInitClazz(name);
        String packageName = ApkNames.getPackageName(name);
        DLPluginPackage dlPluginPackage = DLPluginManager.getInstance(SdkInit.getContext()).getPackage(packageName);
        if(dlPluginPackage!=null){
            try {
                Object object = getRemoteObject(name, initClazz, dlPluginPackage);
                if (object instanceof SignPluginInitable) {
                    SignPluginInitable initable = (SignPluginInitable) object;
                    initable.init(SdkInit.getContext(),pluginInitOkListener);
                } else {
                    if(pluginInitOkListener!=null){
                        pluginInitOkListener.initError(null,"sdk inner error ! object not initable");
                    }
                    LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! object not initable");
                }
            } catch (Exception e) {
                e.printStackTrace();
                if(pluginInitOkListener!=null){
                    pluginInitOkListener.initError(null,e.getMessage());
                }
                LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error "+ e.getMessage());
            }
        }else{
            if(pluginInitOkListener!=null){
                pluginInitOkListener.initError(null,"sdk inner error ! plugin not loaded");
            }
            LogUtils.getNewInstance("KyInterfacePull").dob("sdk inner error ! plugin not loaded");
        }
    }

    private Object getRemoteObject(String name, String initClazz, DLPluginPackage dlPluginPackage) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class clazz = null;
        if(!BuildConfig.isWeb) {
            clazz = Class.forName(initClazz, true, dlPluginPackage.classLoader);
        }else{
            File dexOutputDir = SdkInit.getContext().getDir("dex", Context.MODE_PRIVATE);
            String dexOutputPath = dexOutputDir.getAbsolutePath();
            String mNativeLibDir = SdkInit.getContext().getDir("pluginlib", Context.MODE_PRIVATE).getAbsolutePath();
            String path = LocalPluginMager.getInstance().getLocalPluginBean(name).installPath;
            LogUtils.getNewInstance("KyInterfacePull").eob("path:"+path+" 是否存在"+new File(path).exists());
            DexClassLoader loader = new DexClassLoader(path, dexOutputPath, mNativeLibDir, this.getClass().getClassLoader());
            clazz = Class.forName(initClazz, true, loader);
        }
        return clazz.newInstance();
    }

}
